home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / appl / qwriter / inet.c < prev    next >
C/C++ Source or Header  |  1993-04-13  |  4KB  |  187 lines

  1. /*
  2.  * Opens requred sockets
  3.  * $Header: inet.c,v 2.1 93/04/13 21:46:08 jraja Exp $
  4.  * $Log:    inet.c,v $
  5.  * Revision 2.1  93/04/13  21:46:08  21:46:08  jraja (Jarno Tapio Rajahalme)
  6.  * Made this compile with the newest API.
  7.  * 
  8.  * Revision 2.0  93/03/20  17:31:39  17:31:39  ppessi (Pekka Pessi)
  9.  * initial netlib version..
  10.  * 
  11.  * Revision 1.4  93/03/16  19:13:57  19:13:57  too (Tomi Ollila)
  12.  * code fixes
  13.  * 
  14.  * Revision 1.3  93/03/16  10:43:25  10:43:25  puhuri (Markus Peuhkuri)
  15.  * Added AMITCP stuff.
  16.  * 
  17.  */
  18.  
  19. #ifdef AMIGA
  20. #include <exec/types.h>
  21. #include <sys/types.h>
  22. #if __SASC
  23. #include <proto/socket.h>
  24. #elif __GNUC__
  25. #include <inline/socket.h>
  26. #else
  27. #include <clib/socket_protos.h>
  28. #endif
  29. #endif /* AMIGA */
  30.  
  31. #ifdef __STDC__
  32. #include <stdlib.h>
  33. #endif
  34.  
  35. #include <stdio.h>
  36.  
  37. #include <sys/types.h>
  38. #include <sys/socket.h>
  39. #include <sys/ioctl.h>
  40. #include <sys/time.h>
  41.  
  42. #include <netinet/in.h>
  43.  
  44. #include <netdb.h>
  45.  
  46. #include "qwriter.h"
  47. #include <string.h>
  48.  
  49. #include <arpa/inet.h>
  50.  
  51. /*
  52.  * hostname pointer to our host name or NULL
  53.  * port in a port number to listen
  54.  * type is SOCK_STREAM (TCP) or SOCK_DGRAM (UDP)
  55.  * 
  56.  * returns a file descriptor to open socket
  57.  */
  58.  
  59. char *MyHost;
  60.  
  61. int Fail(int s, int error)
  62. {
  63.   if (s >= 0)
  64.     CloseSocket(s);
  65.   return error;
  66. }
  67.  
  68. int open_server(char *hostname, UWORD port, int type)
  69. #ifndef NODB
  70.   struct hostent *hp;
  71. #endif
  72.   struct sockaddr_in soc_in_s, soc_in_c;
  73.   int soc_len = sizeof(struct sockaddr);
  74.   int sid_s, sid_c;
  75.  
  76.   printf("'%s' port=%ld type=%ld\n", hostname, port, type);
  77. #ifndef NODB
  78.   if(hostname == NULL){
  79.     hostname = (char *)malloc(80);
  80.     gethostname(hostname,80);
  81.   }
  82.                 /* Get our address */
  83.   if ((hp = gethostbyname(hostname)) == NULL){
  84.     fprintf(stderr,"Couldn't get information for host '%s'",hostname);
  85.     return(FAIL);
  86.   }
  87. #endif
  88.                 /* Get socket */
  89.   if ((sid_s = socket(AF_INET, type, 0)) < 0) {
  90.     perror("socket() failed");
  91.     return(FAIL);
  92.   }
  93.                 /* Create address corresponding our service */
  94.  
  95.   bzero((caddr_t)&soc_in_s, sizeof(soc_in_s));
  96.   bzero((caddr_t)&soc_in_c, sizeof(soc_in_c));
  97.  
  98. #ifdef AMITCP
  99.   soc_in_s.sin_len = sizeof(struct sockaddr_in);
  100. #endif
  101.   soc_in_s.sin_family = AF_INET;
  102.   soc_in_s.sin_port = htons(port);
  103. #ifndef NODB
  104.   bcopy(hp->h_addr, (caddr_t)&soc_in_s.sin_addr, hp->h_length);
  105.   printf("addr: %s\n", inet_ntoa(soc_in_s.sin_addr));
  106. #else
  107.   soc_in_s.sin_addr.s_addr = inet_addr(MyHost);
  108. #endif
  109.  
  110.                 /* Bind it to our socket */
  111.   if(bind(sid_s, (struct sockaddr *)&soc_in_s, sizeof(soc_in_s)) < 0 ) {
  112.     perror("bind() failed");
  113.     return (Fail(sid_s, FAIL));
  114.   }
  115.                 /* An UDP-socket can't be
  116.                    listen'd or accept'd */
  117.   if(type == SOCK_DGRAM)
  118.     return (sid_s);
  119.                 /* Listen socket.. */
  120.   if(listen(sid_s, 1) < 0) {
  121.     perror("listen() failed\n");
  122.     return (Fail(sid_s, FAIL));
  123.   }
  124.                 /* Accept connection */
  125.   if ((sid_c = accept(sid_s, 
  126.               (struct sockaddr *)&soc_in_c, 
  127.               (long *)&soc_len)) 
  128.       < 0){
  129.     perror("accept() failed\n");
  130.     return (Fail(sid_s, FAIL));
  131.   }
  132.  
  133.   CloseSocket(sid_s);
  134.  
  135.   return (sid_c);
  136. }
  137.  
  138. /*
  139.  * as above, but does not accept null as hostname
  140.  */
  141. int open_client(char *hostname,UWORD port,int type)
  142. {
  143. #ifndef NODB
  144.   struct hostent *hp;
  145. #endif
  146.   struct sockaddr_in soc_in_s, soc_in_c;
  147.   int sid_s;
  148.  
  149. printf("'%s' port=%ld type=%ld\n",hostname,port,type);
  150.                 /* Get server address */
  151. #ifndef NODB
  152.   if((hp = gethostbyname(hostname)) == NULL){
  153.     fprintf(stderr,"Couldn't get information for host '%s'",hostname);
  154.     perror("");
  155.     return(FAIL);
  156.   }
  157. #endif
  158.                 /* Get socket */
  159.   if((sid_s = socket(AF_INET, type, 0)) < 0) {
  160.     perror("socket() failed");
  161.     return(FAIL);
  162.   }
  163.  
  164.   bzero((caddr_t)&soc_in_s, sizeof(soc_in_s));
  165.   bzero((caddr_t)&soc_in_c, sizeof(soc_in_c));
  166.  
  167. #ifdef AMITCP
  168.   soc_in_s.sin_len = sizeof(struct sockaddr_in);
  169. #endif
  170.   soc_in_s.sin_family = AF_INET;
  171.   soc_in_s.sin_port = htons(port);
  172. #ifndef NODB
  173.   bcopy(hp->h_addr, (caddr_t)&soc_in_s.sin_addr, hp->h_length);
  174. #else
  175.   soc_in_s.sin_addr.s_addr = inet_addr(MyHost);
  176. #endif
  177.  
  178.                 /* Connect to server.. */
  179.   if (connect(sid_s, (struct sockaddr *)&soc_in_s, sizeof(soc_in_s)) < 0){
  180.     perror("connect() failed");
  181.     CloseSocket(sid_s);
  182.     return(FAIL);
  183.   }
  184.   return(sid_s);
  185. }
  186.